package ruide_info_mvc.base.core.check;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import ruide_info_mvc.base.core.exception.customException.CommonException;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName BaseCheckParams
 * @Author wangBo
 * @Date 2019/7/21 0021 下午 12:08
 * @Version 1.0
 */
@Service(value = "baseCheckParams")
public class BaseCheckParams<T> implements IBaseCheckParams<T> {

    private final Logger logger = LoggerFactory.getLogger(BaseCheckParams.class);

    @Override
    public boolean checkParams(T t, Map<String, String> paramsMap) throws CommonException {
        if (null != paramsMap && !paramsMap.isEmpty()) {
            Set<String> keySet = paramsMap.keySet();
            String value;
            for (String key : keySet) {
                try {
                    value = BeanUtils.getProperty(t , key);
                    if (null == value || value.length() == 0) {
                        throw new CommonException(paramsMap.get(key) , Boolean.TRUE);
                    }
                } catch (IllegalAccessException e) {
                    logger.error("" , e);
                    throw new CommonException("参数异常!" , Boolean.FALSE);
                } catch (InvocationTargetException e) {
                    logger.error("" , e);
                    throw new CommonException("参数异常!" , Boolean.FALSE);
                } catch (NoSuchMethodException e) {
                    logger.error("" , e);
                    throw new CommonException("参数异常!" , Boolean.FALSE);
                }
            }
        }
        return false;
    }
}
